<?php
namespace BoxApi\Controller;

use Think\Controller;

class GitController extends CommonController {

	function index() {
		$payload = $_POST;
		$payload = json_decode($payload['hook'], true);
		Logs("gitHook", $payload);
		// $data = 'a:3:{s:8:"password";s:6:"boxphp";s:9:"hook_name";s:10:"push_hooks";s:9:"push_data";a:9:{s:6:"before";s:40:"36263cd5a82c02016bf94749075a689495df5fde";s:5:"after";s:40:"f9155801174d6520380589addeccc4a0d44f0a10";s:3:"ref";s:17:"refs/heads/master";s:9:"user_name";s:9:"温作以";s:4:"user";a:3:{s:5:"email";s:16:"359945126@qq.com";s:4:"name";s:9:"温作以";s:4:"time";s:25:"2016-12-06T21:07:16+08:00";}s:10:"repository";a:4:{s:4:"name";s:4:"demo";s:3:"url";s:35:"git@git.oschina.net:wen190/demo.git";s:11:"description";s:0:"";s:8:"homepage";s:34:"http://git.oschina.net/wen190/demo";}s:7:"commits";a:1:{i:0;a:5:{s:2:"id";s:40:"f9155801174d6520380589addeccc4a0d44f0a10";s:7:"message";s:17:"更新 index.html";s:9:"timestamp";s:25:"2016-12-06T21:07:16+08:00";s:3:"url";s:82:"http://git.oschina.net/wen190/demo/commit/f9155801174d6520380589addeccc4a0d44f0a10";s:6:"author";a:3:{s:4:"name";s:9:"温作以";s:5:"email";s:16:"359945126@qq.com";s:4:"time";s:25:"2016-12-06T21:07:16+08:00";}}}s:19:"total_commits_count";i:1;s:21:"commits_more_than_ten";b:0;}}';
		// $payload = (unserialize($data));
		// dump($payload);

		// $data = 'a:3:{s:8:"password";s:6:"boxphp";s:9:"hook_name";s:10:"push_hooks";s:9:"push_data";a:9:{s:6:"before";s:40:"0000000000000000000000000000000000000000";s:5:"after";s:40:"ec7159240a346fa5988913aa3057b902a4acb126";s:3:"ref";s:17:"refs/heads/master";s:9:"user_name";s:9:"温作以";s:4:"user";a:4:{s:2:"id";i:554961;s:5:"email";s:16:"359945126@qq.com";s:4:"name";s:9:"温作以";s:4:"time";s:25:"2016-12-06T20:45:39+08:00";}s:10:"repository";a:4:{s:4:"name";s:5:"demo2";s:3:"url";s:40:"https://git.oschina.net/wen190/demo2.git";s:11:"description";s:0:"";s:8:"homepage";s:35:"http://git.oschina.net/wen190/demo2";}s:7:"commits";a:1:{i:0;a:5:{s:2:"id";s:40:"ec7159240a346fa5988913aa3057b902a4acb126";s:7:"message";s:19:"A Test For WebHooks";s:9:"timestamp";s:25:"2015-11-06T13:21:07+08:00";s:3:"url";s:83:"http://git.oschina.net/wen190/demo2/commit/ec7159240a346fa5988913aa3057b902a4acb126";s:6:"author";a:3:{s:4:"name";s:9:"温作以";s:5:"email";s:16:"359945126@qq.com";s:4:"time";s:25:"2015-11-06T13:21:07+08:00";}}}s:19:"total_commits_count";i:2;s:21:"commits_more_than_ten";b:0;}}';
		// $payload = (unserialize($data));

		$repository = $payload['push_data']['repository'];
		$repository['full_name'] = str_replace("http://git.oschina.net/", "", $repository['homepage']);
		$repository['clone_url'] = $repository['url'];
		//exit();
		$dir = "/wen/www/git/";
		$dirs = explode("/", $repository['full_name']);
		$name = $dirs[0];
		$full_dir = $dir . $repository['full_name'];
		$my_dirs = $dir . $name;
		$clone_url = $repository['clone_url'];
		//只支持github
		$urlInfo = parse_url($repository['homepage']);
		//dump($urlInfo);
		if ($urlInfo['host'] != "git.oschina.net") {
			Logs("gitHookError", $clone_url);
			exit();
		}

		$user_name = $_GET['user_name'];
		$password = $_GET['password'];
		if ($user_name && $password) {
			$clone_url = str_replace("https://", "https://" . $user_name . ":" . $password . "@", $clone_url);
		}
		//存在项目
		if (is_dir($full_dir)) {
			$cmd = " cd {$full_dir} && git pull";
		} else {
			//存在个人目录
			if (is_dir($my_dirs)) {
				$cmd = " cd {$my_dirs}  && git clone {$clone_url}";
			} else {
				$cmd = " cd {$dir} && mkdir {$name} &&  cd {$my_dirs} && git clone {$clone_url}"; //不存存个人目录
			}
		}
		$r = shell_exec($cmd);
		Logs("gitHook", array('cmd' => $cmd, 'ret' => $r));

	}

	function hub() {
		$data = json_decode(file_get_contents('php://input'), true);
		$repository = $data['repository'];
		if (!$repository) {
			return;
		}
		Logs("gitHook", $data);
		$dir = "/wen/www/git/";
		$dirs = explode("/", $repository['full_name']);
		$name = $dirs[0];
		$full_dir = $dir . $repository['full_name'];
		$my_dirs = $dir . $name;
		$clone_url = $repository['clone_url'];
		//只支持github
		$urlInfo = parse_url($clone_url);
		if ($urlInfo['host'] != "github.com") {
			Logs("gitHookError", $urlInfo);
			exit();
		}
		//存在项目
		if (is_dir($full_dir)) {
			$cmd = " cd {$full_dir} && git pull";
		} else {
			//存在个人目录
			if (is_dir($my_dirs)) {
				$cmd = " cd {$my_dirs}  && git clone {$clone_url}";
			} else {
				$cmd = " cd {$dir} && mkdir {$name} &&  cd {$my_dirs} && git clone {$clone_url}"; //不存存个人目录
			}
		}
		$r = shell_exec($cmd);
		Logs("gitHook", $r);
	}

	function cmd() {

	}

}